<template>
  <div class="order-container">
    <el-tabs @tab-click="tabClick">
      <!-- tab切换 -->
      <el-tab-pane v-for="item in tabTypes" :key="item.name" :label="item.label"/>

      <div class="main-container">
        <div class="holder-container" v-if="orderList.length === 0">
          <el-empty description="暂无订单数据"/>
        </div>
        <div v-else>
          <!-- 订单列表 -->
          <div class="order-item" v-for="order in orderList" :key="order.id">
            <div class="head">
              <span>下单时间：{{ order.createTime }}</span>
              <span>订单编号：{{ order.id }}</span>
              <!-- 未付款，倒计时时间还有 -->
              <!--                <span class="down-time" v-if="order.orderState === 1">-->
              <!--                    <i class="iconfont icon-down-time"></i>-->
              <!--                    <b>付款截止: {{order.countdown}}</b>-->
              <!--                </span>-->
            </div>
            <div class="body">
              <div class="column goods">
                <ul>
                  <li v-for="item in order.medicineVOS" :key="item.id">
                    <a class="image" href="javascript:;">
                      <img :src="item.image" alt=""/>
                    </a>
                    <div class="info">
                      <p class="name ellipsis-2">
                        {{ item.name }}
                      </p>
                      <p class="attr ellipsis">
                        <span>{{ item.attrsText }}</span>
                      </p>
                    </div>
                    <div class="price">¥{{ item.price?.toFixed(2) }}</div>
                    <div class="count">x{{ item.count }}</div>
                  </li>
                </ul>
              </div>
              <div class="column state">
                <div v-if="order.cancel == 0">
                  <p>{{ fomartPayState(order.status) }}</p>
                  <p v-if="order.status == 0">
                    <el-button type="text" @click="Upload(order.id)">上传处方</el-button>
                  </p>
                  <p v-if="order.status == 1">
                    <el-button type="text" @click="open(order.image)">查看处方</el-button>
                  </p>
                  <p v-if="order.status == 2&& order.image!=undefined">
                    <el-button type="text" @click="open(order.image)">查看处方</el-button>
                  </p>
                  <p v-if="order.status == 3">
                    <el-button type="text" @click="dialogFormVisible = true">重新审核</el-button>
                  </p>
                  <p>
                    <el-button type="text" @click="cancel(order.id)">取消订单</el-button>
                  </p>
                </div>
                <div v-if="order.cancel == 1">
                  <p>已取消</p>
                </div>
                <div v-if="order.cancel == 2">
                  <p>已完成</p>
                </div>
              </div>
              <div class="column amount">
                <p class="red">总计：{{ order.price?.toFixed(2) }}</p>
              </div>
            </div>
          </div>
        </div>
      </div>

    </el-tabs>

    <el-dialog title="上传处方" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-upload
            class="avatar-uploader"
            :action="action()"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :on-change="onChange"
            ref="upload"
        >
          <img
              v-if="form.image"
              :src="form.image"
              style="height: 200px;width: 200px"
              class="avatar"
          ></img>
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="onSubmit()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="处方图片" :visible.sync="dialogFormVisible1">
      <el-image
          style="height: 500px"
          :src="image"></el-image>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogFormVisible1=false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapGetters} from 'vuex';
import {Audit, cancel, getUserOrder} from "@/api/order";
import {getGoodsAPI} from "@/api/home";

export default {
  name: 'UserOrder',
  data() {
    return {
      image: "",
      dialogFormVisible1: false,
      dialogFormVisible: false,
      form: {
        id: "",
        image: ""
      },
      formLabelWidth: '120px',
      // tab列表
      tabTypes: [
        {name: "-1", label: "全部订单"},
        {name: "0", label: "进行中"},
        {name: "1", label: "已取消"},
        {name: "2", label: "已完成"},
      ],
      // 获取的我的订单列表的参数
      params: {
        // total: 10,
        // size: 5,
        // current: 1
        cancel: -1
      },
      stateMap: {
        0: '进行中',
        1: '已取消',
        2: '已完成'
      }
    }
  },
  mounted() {
    this.getOrderList();
  },
  computed: {
    ...mapGetters('user', ['orderList', 'counts']),
  },
  methods: {
    async cancel(id) {
      this.$confirm('确定要取消订单吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await cancel({id:id});
        this.getOrderList();
        console.log(id)
        this.$message({
          type: 'success',
          message: '取消成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消失败'
        });
      });
    },
    open(image) {
      this.image = image
      this.dialogFormVisible1 = true
    },
    close() {
      this.dialogFormVisible = false
      this.form.id = ""
      this.form.image = ""
      this.getOrderList();
    },
    async onSubmit() {
      await Audit(this.form);
      this.dialogFormVisible = false
      this.form.id = ""
      this.form.image = ""
      this.getOrderList();
    },

    action() {
      return "http://localhost:8081/common/upload"
    },
    handleAvatarSuccess(response, file, fileList) {
      // 拼接down接口预览
      // this.imageUrl = `/common/download?name=${response.data}`;
      // console.info("sakura");
      // console.info(response.data)
      this.form.image = "http://localhost:8081/common/download?name=" + response.data
      // console.info(this.form.image)
    },

    onChange(file) {
      if (file) {
        const suffix = file.name.split('.')[1]
        const size = file.size / 1024 / 1024 < 2
        if (['png', 'jpeg', 'jpg'].indexOf(suffix) < 0) {
          this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
          this.$refs.upload.clearFiles()
          return false
        }
        if (!size) {
          this.$message.error('上传文件大小不能超过 2MB!')
          return false
        }
        return file
      }
    },
    Upload(id) {
      this.form.id = id;
      this.dialogFormVisible = true
    },
    getOrderList() {
      this.$store.dispatch('user/getOrderList', this.params);
    },
    // tab切换
    tabClick(type) {
      // console.log(type.index)
      this.params.cancel = type.index - 1;
      this.getOrderList();
    },
    // 当前页切换
    pageChange(page) {
      this.params.page = page;
      this.getOrderList()
    },
    // 创建格式化函数
    fomartPayState(payState) {
      const stateMap = {
        0: '未审核',
        1: '审核中',
        2: '审核成功',
        3: '审核驳回'
      }
      return stateMap[payState]
    }
  }
}
</script>

<style scoped lang="scss">
.order-container {
  padding: 10px 20px;

  .pagination-container {
    display: flex;
    justify-content: center;
  }

  .main-container {
    min-height: 500px;

    .holder-container {
      min-height: 500px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}

.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;

  .head {
    height: 50px;
    line-height: 50px;
    background: #f5f5f5;
    padding: 0 20px;
    overflow: hidden;

    span {
      margin-right: 20px;

      &.down-time {
        margin-right: 0;
        float: right;

        i {
          vertical-align: middle;
          margin-right: 3px;
        }

        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }

    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }

  .body {
    display: flex;
    align-items: stretch;

    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;

      > p {
        padding-top: 10px;
      }

      &:first-child {
        border-left: none;
      }

      &.goods {
        flex: 1;
        padding: 0;
        align-self: center;

        ul {
          li {
            border-bottom: 1px solid #f5f5f5;
            padding: 10px;
            display: flex;

            &:last-child {
              border-bottom: none;
            }

            .image {
              width: 70px;
              height: 70px;
              border: 1px solid #f5f5f5;
            }

            .info {
              width: 220px;
              text-align: left;
              padding: 0 10px;

              p {
                margin-bottom: 5px;

                &.name {
                  height: 38px;
                }

                &.attr {
                  color: #999;
                  font-size: 12px;

                  span {
                    margin-right: 5px;
                  }
                }
              }
            }

            .price {
              width: 100px;
            }

            .count {
              width: 80px;
            }
          }
        }
      }

      &.state {
        width: 120px;

        .green {
          color: $xtxColor;
        }
      }

      &.amount {
        width: 200px;

        .red {
          color: $priceColor;
        }
      }

      &.action {
        width: 140px;

        a {
          display: block;

          &:hover {
            color: $xtxColor;
          }
        }
      }
    }
  }
}
</style>
